﻿using TripleGame.Core;
using System.Reflection;
using System;

namespace TripleGame.GUI
{
    class Player
    {
        public int Score { get; set; }
        public char Letter { get; set; }
        public ITripleGameEngine Engine { get; private set; }

        public void LoadEngine(string fileName)
        {
            Assembly engineAssembly = Assembly.LoadFrom(fileName);
            Type[] assemblyTypes = engineAssembly.GetTypes();
            string tripleGameEngineInterface = typeof(ITripleGameEngine).Name;
            bool engineLoaded = false;
            foreach (Type type in assemblyTypes)
            {
                if (type.GetInterface(tripleGameEngineInterface) != null)
                {
                    this.Engine = (ITripleGameEngine)Activator.CreateInstance(type);
                    engineLoaded = true;
                }
            }
            if (!engineLoaded)
            {
                throw new InvalidOperationException(
                    "Can not find a class implementing " + tripleGameEngineInterface + 
                    " interface in assembly " + engineAssembly.FullName);
            }
        }
    }
}
